﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using System.Windows.Forms;
using Krypton.Docking;
using Krypton.Navigator;
using Krypton.Toolkit;
using Krypton.Workspace;
using NesEmu.Explorer.Pages;

namespace NesEmu.Explorer
{
    public partial class FrmMain : KryptonForm
    {
        private EmulatorPage.Page? _emulatorPage;

        public FrmMain()
        {
            InitializeComponent();
        }
        internal event EventHandler<BroadcastEventArgs>? OnMessageBroadcasted;

        internal TPage? AddOrShowWorkspacePage<TPage>(string uniqueName, object? initArgs = null) where TPage : KryptonPage
        {
            var cell = kryptonDockableWorkspace.ActiveCell;
            if (cell == null || kryptonDockableWorkspace.Root.Children?.Count == 0)
            {
                cell = new KryptonWorkspaceCell();
                kryptonDockableWorkspace.Root.Children?.Add(cell);
            }

            if (cell.Pages.Any(p => p.UniqueName == uniqueName))
            {
                var page = cell.Pages[uniqueName];
                if (page is not null)
                {
                    page.Show();
                    if (kryptonDockingManager.PagesFloating.Any(p => p.UniqueName == uniqueName))
                    {
                        kryptonDockingManager.ShowPage(page);
                        page.BringToFront();
                    }
                    else
                    {
                        cell.SelectedPage = page;
                    }

                    return (TPage)page;
                }
            }
            else
            {
                var page = (KryptonPage?)Activator.CreateInstance(typeof(TPage), this, initArgs);
                if (page is not null)
                {
                    page.UniqueName = uniqueName;
                    page.ClearFlags(KryptonPageFlags.DockingAllowAutoHidden | KryptonPageFlags.DockingAllowDocked);
                    cell.Pages.Add(page);
                    cell.SelectedPage = page;

                    return (TPage)page;
                }
            }

            return null;
        }

        internal void Broadcast(object sender, string message, object? args)
        {
            HandleMessage(message, args);
            OnMessageBroadcasted?.Invoke(sender, new BroadcastEventArgs(message, args));
        }

        

        private void HandleMessage(string message, object? args)
        {

        }

        private void ActionOpenDirectory(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog(this) == DialogResult.OK)
            {
                Broadcast(this, "main.directoryOpen", folderBrowserDialog.SelectedPath);
            }
        }

        private void ActionShowFileBrowser(object sender, EventArgs e)
        {
            kryptonDockingManager.ShowPage(FileBrowserPage.Page.PageName);
        }

        private void ActionShowEmulator(object sender, EventArgs e)
        {
            _emulatorPage = AddOrShowWorkspacePage<EmulatorPage.Page>(EmulatorPage.Page.PageName);
        }

        private void ActionShowFileProperty(object sender, EventArgs e)
        {
            kryptonDockingManager.ShowPage(FilePropertyPage.Page.PageName);
        }

        protected override void OnLoad(EventArgs e)
        {
            var fileBrowserPage = new FileBrowserPage.Page(this);
            var filePropertyPage = new FilePropertyPage.Page(this);
            _emulatorPage = new EmulatorPage.Page(this);

            base.OnLoad(e);
            var dockingWorkspace =
                kryptonDockingManager.ManageWorkspace("mainDockingWorkspace", kryptonDockableWorkspace);
            kryptonDockingManager.ManageControl("mainDockingControl", pnlMain, dockingWorkspace);
            kryptonDockingManager.ManageFloating("mainFloating", this);
            kryptonDockingManager.AddToWorkspace("mainDockingWorkspace", new KryptonPage[] { _emulatorPage });
            var dockspace = kryptonDockingManager.AddDockspace("mainDockingControl", DockingEdge.Left,
                new KryptonPage[] { fileBrowserPage }, new KryptonPage[] { filePropertyPage });
            dockspace.DockspaceControl.MinimumSize = dockspace.DockspaceControl.MinimumSize with { Width = Width / 3 };
        }
    }
}
